package org.fastsyncer.rest.controller;

import javax.servlet.http.HttpServletResponse;

import org.fastsyncer.common.entity.RestResult;
import org.fastsyncer.framework.manager.Manager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/index")
public class IndexController {

	private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

	@RequestMapping("")
	public String index(ModelMap model) {
		logger.info("正在访问首页.");
		return "index/index";
	}

	@RequestMapping(value = "/getDriverAll")
	@ResponseBody
	public RestResult getDriverAll(HttpServletResponse response) {
		try {
			return RestResult.restSuccess(Manager.getInstance().getDriverAll());
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage(), e.getClass());
			return RestResult.restFail(e.getLocalizedMessage());
		}
	}

	/**
	 * 获取正在排队的同步任务
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getQueue")
	@ResponseBody
	public RestResult getQueue() {
		try {
			return RestResult.restSuccess(Manager.getInstance().getQueue());
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage(), e.getClass());
			return RestResult.restFail(e.getLocalizedMessage());
		}
	}

	/**
	 * 获取正在运行的同步任务
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getRunning")
	@ResponseBody
	public RestResult getRunning() {
		try {
			return RestResult.restSuccess(Manager.getInstance().getRunning());
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage(), e.getClass());
			return RestResult.restFail(e.getLocalizedMessage());
		}
	}

}
